package com.wngbms.websocket.controller;

import com.wngbms.websocket.user.UserHook;
import com.wngbms.websocket.zookeeper.UserNodeNotifier;
import com.wngbms.websocket.zookeeper.ZkConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class TriggerController implements ZkConst {

    @Autowired
    UserNodeNotifier userNodeNotifier;
    @Autowired
    UserHook userHook;

    @GetMapping("/notify/{userId}/{eventId}")
    public String notify(@PathVariable(name = "userId") String userId,
                         @PathVariable(name = "eventId") String eventId) {
        try {
            userNodeNotifier.notifyUser(userId, eventId);
        } catch (Exception e) {
            log.error("notify error", e);
            return FAILED;
        }
        return SUCCESS;
    }

    @GetMapping("/closeUser/{userId}")
    public String forceCloseUser(@PathVariable("userId") String userId) {
        try {
            userHook.closeUserSession(userId);
        } catch (Exception e) {
            log.error("close user error", e);
            return FAILED;
        }
        return SUCCESS;
    }

}
